﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using HeartLess.LevelData;
using HeartLess.Core;
using HeartLess.Managers;
using Microsoft.Xna.Framework.Graphics;

namespace HeartLess.GameObjects
{
    class HeartRateModifier : Item
    {
        private Animation _animation;
        private Single _hrDelta;
        private Single _delta;

        private Player _target;

        public HeartRateModifier(Player target, Point location, Single hrDelta, Single deltaSec)
            : base(location)
        {
            Texture2D animationTexture = AssetManager.GetAnimationTexture(AssetManager.ANIMATIONS.HEART_RATE_MODIFIER);
            _animation = new Animation(animationTexture, 32, 60);
            _animation.Location = this.Location;

            _target = target;
            _hrDelta = hrDelta;
            _delta = deltaSec;
        }

        public override void Update(GameTime gameTime)
        {
            _animation.Update(gameTime);

            base.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            _animation.Draw(spriteBatch);
        }

        public static void ChangeHeartRate(Player player, Single hrDelta, Single applyTime)
        {
            int deltaMSec = (int)Math.Round(applyTime * 1000.0f);
            TimeSpan delta = new TimeSpan(0, 0, 0, deltaMSec / 1000, deltaMSec % 1000);
            player.HeartRate = player.HeartRate + hrDelta;
            player.AddModifier(new Modifier(hrDelta, delta));
        }

        public static void ChangeHeartRateFactor(Player player, Single hrDelta, Single applyTime)
        {
            ChangeHeartRate(player, player.HeartRate * hrDelta, applyTime);
        }

        protected override void handleCollision(CollidableGameObject collidedObject)
        {
            if (collidedObject == _target)
            {
                ChangeHeartRateFactor(_target, _hrDelta, _delta);
                Dead = true;
            }
        }

        private class Modifier : Player.Modifier
        {
            Single _hrDelta;

            public Modifier(Single hrDelta, TimeSpan delta)
                : base(delta)
            {
                _hrDelta = hrDelta;
            }

            public override void OnExpiration(Player player)
            {
                player.HeartRate = player.HeartRate - _hrDelta;
            }
        }
    }
}
